package com.hcs.chenyu;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Map;

public class registermain extends AppCompatActivity implements View.OnClickListener {
    private Button mqqdlbtn_login5,mqqdlbtn_login6;
    private CheckBox checkBox_1,checkBox_2,checkBox_3;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registermain);
        initView();
        findView();            //获取页面中的控件
        setListener();        //设置控件的监听器

        mqqdlbtn_login5 = findViewById(R.id.btn_login6);

        final TextView viewById = findViewById(R.id.tv_7);
        final TextView viewById1 = findViewById(R.id.et_name1);
        final TextView viewById2 = findViewById(R.id.et_password1);
        final TextView viewById3 = findViewById(R.id.et_password2);
        mqqdlbtn_login6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = viewById1.getText().toString().trim();
                String password = viewById2.getText().toString().trim();

                if (TextUtils.isEmpty(account)){
                    Toast.makeText(registermain.this, "请输账号", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(password)){
                    Toast.makeText(registermain.this, "请输密码", Toast.LENGTH_SHORT).show();
                    return;}


                else {
                    Toast.makeText(registermain.this, "登陆成功", Toast.LENGTH_SHORT).show();
                }

                boolean isSaveSuccess = administrationDatabase.savaUserInfo(registermain.this, account, password);

                if (isSaveSuccess){
                    Toast.makeText(registermain.this, "保存成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(registermain.this, "保存失败", Toast.LENGTH_SHORT).show();
                }

                if (!TextUtils.isEmpty(viewById1.getText())&&!TextUtils.isEmpty(viewById2.getText())&&!TextUtils.isEmpty(viewById3.getText())) {
                    viewById.setText("姓名：" + viewById1.getText().toString() + "\n" + "密碼：" + viewById2.getText() + "確認密碼：" + viewById3.getText().toString() + "\n");

                }
                else if (TextUtils.isEmpty(viewById1.getText())) {
                    viewById.setText("你的用户名输入有误" );
                    AlertDialog dialog;
                    AlertDialog.Builder builder = new AlertDialog.Builder(registermain.this)
                            .setTitle("普通对话框").setIcon(R.drawable.ic_launcher_background)
                            .setMessage("您的用户名输入有误，请重新输入")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    finish();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                  dialog.dismiss();
                                }
                            });
                    dialog = builder.create();
                    dialog.show();
                    Toast.makeText(registermain.this,"请您输入用户名",Toast.LENGTH_LONG).show();

                }
                else if (TextUtils.isEmpty(viewById2.getText())) {
                    viewById.setText("你的密码输入有误" );
                    AlertDialog dialog;
                    AlertDialog.Builder builder = new AlertDialog.Builder(registermain.this)
                            .setTitle("普通对话框").setIcon(null)
                            .setMessage("您的密码输入有误，请重新输入")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    finish();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();


                                }
                            });
                    dialog = builder.create();
                    dialog.show();
                    Toast.makeText(registermain.this,"请您输入密码",Toast.LENGTH_LONG).show();
                }
                else if (TextUtils.isEmpty(viewById3.getText())) {
                    viewById.setText("你的确认密码输入有误" );

                    Toast.makeText(registermain.this,"请您输入确认密码",Toast.LENGTH_LONG).show();
                }
            }
        });

    }

    private void setListener() {
        // TODO Auto-generated method stub
        //设置所有CheckBox的状态改变监听器
        checkBox_1.setOnCheckedChangeListener(myCheckChangelistener);
        checkBox_2.setOnCheckedChangeListener(myCheckChangelistener);
        checkBox_3.setOnCheckedChangeListener(myCheckChangelistener);

    }

    CompoundButton.OnCheckedChangeListener myCheckChangelistener = new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            //设置TextView的内容显示CheckBox的选择结果
            setText();
        }
    };


    private void findView() {
        // TODO Auto-generated method stub
        //通过findViewById得到对应的控件对象
        checkBox_1 = (CheckBox)findViewById(R.id.ck_1);
        checkBox_2 = (CheckBox)findViewById(R.id.ck_2);
        checkBox_3 = (CheckBox)findViewById(R.id.ck_3);
        textView = (TextView)findViewById(R.id.tv_6);
        View viewById = findViewById(R.id.et_name1);

    }

    private void setText(){
        String str;
        textView.setText("");    //清空TextView的内容
        //如果CbBasketball被选中，则加入TvResult内容显示
        if (checkBox_1.isChecked()) {
            str = textView.getText().toString()+checkBox_1.getText().toString()+",";
            textView.setText(str);
        }
        //如果CbPingpangball被选中，则加入TvResult内容显示
        if (checkBox_2.isChecked()) {
            str = textView.getText().toString()+checkBox_2.getText().toString()+",";
            textView.setText(str);
        }
        //如果CbFootball被选中，则加入TvResult内容显示
        if (checkBox_3.isChecked()) {
            str = textView.getText().toString()+checkBox_3.getText().toString();
            textView.setText(str);
        }
    }





    private void initView() {
        mqqdlbtn_login5 = findViewById(R.id.btn_login5);
        mqqdlbtn_login6 = findViewById(R.id.btn_login6);
        checkBox_1 =findViewById(R.id.ck_1);
        checkBox_2 =findViewById(R.id.ck_2);
        checkBox_3 = findViewById(R.id.ck_3);
        textView =findViewById(R.id.tv_5);
        mqqdlbtn_login5.setOnClickListener(this);



    }

    @Override
    public void onClick(View v) {
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("普通对话框").setIcon(R.drawable.ic_launcher_foreground)
                .setMessage("是否返回上一页面")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

}

